//
//  BBSideViewController.m
//  BabyThermometer
//
//  Created by CaiLei on 10/2/14.
//  Copyright (c) 2014 baohu. All rights reserved.
//

#import "BBSideViewController.h"
#import "BBSideItemCell.h"
#import "BBSettingViewController.h"

@interface BBSideViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *itemArray;
@end

@implementation BBSideViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self setupMockData];
    self.itemArray = @[@"发烧助手", @"警报温度", @"推荐给好友", @"育儿圈", @"设置"];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgImageView.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    bgImageView.image = [UIImage imageNamed:@"side_bg"];
    [self.view addSubview:bgImageView];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.autoresizingMask =  UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - table view datasource & delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return [self.babyArray count] + 1; // +1 for 添加宝宝
    }
    
    if (section == 1) {
        return [self.itemArray count];
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 80.f;
    }
    
    if (section == 1) {
        return 0.5f;
    }
    
    if (section == 2) {
        // TBD: 当高度可变时是否应该动态算出? height > screen heigh则固定44, heigh < screen height则screen height - height
        return 94.f;
    }
    return 0.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    if (section == 0) {
        headerView.backgroundColor = [UIColor clearColor];
    }
    
    if (section == 1) {
        headerView.backgroundColor = [[UIColor colorWithHexString:@"3f7997"] colorWithAlphaComponent:0.1];
    }
    
    if (section == 2) {
        self.battoryProgressLabel = [[UILabel alloc] initWithFrame:CGRectMake(33, 94 - 44 - 5 - 5 - 15, 190, 20)];
        self.battoryProgressLabel.textColor = [UIColor colorWithHexString:@"618eb4"];
        self.battoryProgressLabel.font = [UIFont systemFontOfSize:11];
        [headerView addSubview:self.battoryProgressLabel];
        self.battoryProgressLabel.text = @"鱼儿体温计剩余电量0%";
        
        self.battoryProgressView = [[BBBattoryProgressView alloc] initWithFrame:CGRectMake(33, 94 - 44 - 5, 190, 5)];
        [headerView addSubview:self.battoryProgressView];
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60.f;
    }
    
    if (indexPath.section == 1) {
        return 48.f;
    }
    
    return 0.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *babyCellId = @"babyCellId";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:babyCellId];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:babyCellId];
        }
        cell.textLabel.text = @"Test";
        
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }
    
    if (indexPath.section == 1) {
        static NSString *itemCellId = @"itemCellId";
        BBSideItemCell *cell = [tableView dequeueReusableCellWithIdentifier:itemCellId];
        if (!cell) {
            cell = [[BBSideItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:itemCellId];
        }
        NSString *itemInfo = self.itemArray[indexPath.row];
        cell.normalIconName = itemInfo;
        cell.highlightIconName = [itemInfo stringByAppendingString:@"_hl"];
        
        cell.iconImageView.image = [UIImage imageNamed:itemInfo];
        cell.nameLabel.text = itemInfo;
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        cell.selectedBackgroundView.backgroundColor = [[UIColor colorWithHexString:@"78d7f3"] colorWithAlphaComponent:0.15];
        return cell;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NSLog(@"hhhhhhhhhhhh");
        BBSideItemCell *cell = (BBSideItemCell *)[tableView cellForRowAtIndexPath:indexPath];
        NSString *itemInfo = self.itemArray[indexPath.row];
        cell.iconImageView.image = [UIImage imageNamed:[itemInfo stringByAppendingString:@"_hl"]];
        [cell setNeedsDisplay];
        [cell.iconImageView setNeedsDisplay];
        [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow:0.03]];
    }
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NSLog(@"lllllllllllll");
        BBSideItemCell *cell = (BBSideItemCell *)[tableView cellForRowAtIndexPath:indexPath];
        NSString *itemInfo = self.itemArray[indexPath.row];
        cell.iconImageView.image = [UIImage imageNamed:itemInfo];
        [cell setNeedsDisplay];
        [cell.iconImageView setNeedsDisplay];
        [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow:0.03]];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.battoryProgressView.progress = 0.2 * indexPath.row;

    NSString *battoryText = @"鱼儿体温计剩余电量";
    int p = self.battoryProgressView.progress * 100;
    battoryText = [battoryText stringByAppendingString:[NSString stringWithFormat:@" %d", p]];
    battoryText = [battoryText stringByAppendingString:@"%"];
    self.battoryProgressLabel.text = battoryText;
    
    if (indexPath.section == 1) {
        if (indexPath.row == 4) { // 设置
            BBSettingViewController *vc = [[BBSettingViewController alloc] init];
            // TBD: ios7下dismiss有问题
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
            //[self presentFromRight:[[UINavigationController alloc] initWithRootViewController:vc]];
        }
    }
}

#pragma mark - mock
- (void)setupMockData {
    self.babyArray = [NSMutableArray arrayWithArray:@[@"demo baby"]];
}

@end
